package com.huidz.calendar.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.transition.Slide;
import android.view.Gravity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

import com.huidz.calendar.R;
import com.huidz.calendar.activityadapter.YiJiAdapter;
import com.huidz.calendar.mainfragment.CalendarFragment;
import com.huidz.calendar.util.Apiutils;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class YiJiLookActivity extends Activity implements View.OnClickListener {

    private ImageView comeback;
    private TextView look_yiji_texttitle, xdwjieshi, jinqi, day_num, start_time,over_time;
    private ListView yiji_list;
    private Switch kanzhoumo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Slide slide = new Slide(Gravity.RIGHT);
        slide.setDuration(200L);
        getWindow().setEnterTransition(slide);
        setContentView(R.layout.activity_checklook);
        inti();
    }

    ArrayList<String> arrayList = new ArrayList<>();
    ArrayList<String> arrayLists = new ArrayList<>();

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        Intent getindex = getIntent();
        look_yiji_texttitle = findViewById(R.id.look_yiji_texttitle);
        look_yiji_texttitle.setText(getindex.getStringExtra("title"));
        String yj = getindex.getStringExtra("title");
        comeback = findViewById(R.id.title_comeback);
        comeback.setOnClickListener(this);
        xdwjieshi = findViewById(R.id.xdwjieshi);
        yiji_list = findViewById(R.id.yiji_list);
        kanzhoumo=findViewById(R.id.kanzhoumo);
        jinqi = findViewById(R.id.jinqi);
        day_num = findViewById(R.id.day_num);
        start_time = findViewById(R.id.start_time);
        over_time=findViewById(R.id.over_time);
        jinqi.setText("近期" + getindex.getStringExtra("title") + "的日子共有");
        File file = new File(YiJiLookActivity.this.getFilesDir(), "saa.db");
        database = SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
        xdwjieshi.setText(yj + ":" + SelectXDW(yj.substring(1, yj.length())));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        SelectCode(CalendarFragment.DATETIME);
        for (int i = 1; i <= 90; i++) {
            try {
            SelectCode(getDateStr(CalendarFragment.DATETIME, i));
            SelectYiJi(gz, jx, getindex.getStringExtra("title"));
            arrayList.add(getDateStr(CalendarFragment.DATETIME, i));
                if(getWeek(Long.valueOf(dateToStamp(getDateStr(CalendarFragment.DATETIME, i))))){
                    arrayLists.add(getDateStr(CalendarFragment.DATETIME, i));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        day_num.setText("" + arrayList.size());
        start_time.setText(arrayList.get(0));
        over_time.setText(arrayList.get(arrayList.size()-1));
        myAdapter = new YiJiAdapter(arrayList,
                YiJiLookActivity.this,YiJiLookActivity.this);
        yiji_list.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();
        kanzhoumo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    myAdapter = new YiJiAdapter(arrayLists,
                            YiJiLookActivity.this,YiJiLookActivity.this);
                    yiji_list.setAdapter(myAdapter);
                }else{
                    myAdapter = new YiJiAdapter(arrayList,
                            YiJiLookActivity.this,YiJiLookActivity.this);
                    yiji_list.setAdapter(myAdapter);
                }
            }
        });
    }
    public YiJiAdapter myAdapter;
    public static int days = 0;


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_comeback:
                onBackPressed();
                break;
        }
    }

    public static SQLiteDatabase database;

    /**
     * 获取现代文的方法
     *
     * @param ancient 条件查询值
     */
    public static String SelectXDW(String ancient) {
        Cursor mCursor = database.query("explain", null, "ancient=?", new String[]{ancient}, null,
                null, null);
        String getxdw = null;
        while (mCursor.moveToNext()) {
            getxdw = mCursor.getString(1);
        }
        return getxdw;
    }

    /**
     * 获取现代文的方法
     *
     * @param ancient 条件查询值
     */
    public static void getyijitime(String ancient) {
        Cursor mCursor = database.query("DetailHuangLi", null, "Yi0 like '%" + ancient + "%'", null, null,
                null, null);
        while (mCursor.moveToNext()) {
            Apiutils.setlog("time:" + mCursor.getString(0));
//            mCursor.getString(2);
        }
    }

    /**
     * 获取宜忌的方法
     *
     * @param gz 条件查询值，传入时间
     */
    public void SelectYiJi(String gz, String jx, String str) {
        String s = str.substring(1, str.length());
        Cursor mCursor = database.query("YJData", null, "jx=" + jx + " and gz=" + gz + " and yi like '%" + s + "%' ", null, null,
                null, null);
        while (mCursor.moveToNext()) {
            mCursor.getString(3);
            Apiutils.setlog("time:" + mCursor.getString(3));
        }
    }

    public String jx = "", gz = "";

    /**
     * 获取时间的code
     *
     * @param _Date 传入时间
     */
    public void SelectCode(String _Date) {
        Cursor mCursor = database.query("IndexTable", null, "_Date=?", new String[]{_Date}, null,
                null, null);
        while (mCursor.moveToNext()) {
            jx = mCursor.getString(1);
            gz = mCursor.getString(2);
        }
    }

    public static String getDateStr(String day, int Num) {
        days++;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = null;
        try {
            nowDate = df.parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //如果需要向后计算日期 -改为+
        Date newDate2 = new Date(nowDate.getTime() + (long) Num * 24 * 60 * 60 * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateOk = simpleDateFormat.format(newDate2);
        return dateOk;
    }

    public static boolean getWeek(long time) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(time));
        int year = cd.get(Calendar.YEAR); //获取年份
        int month = cd.get(Calendar.MONTH); //获取月份
        int day = cd.get(Calendar.DAY_OF_MONTH); //获取日期
        int week = cd.get(Calendar.DAY_OF_WEEK); //获取星期
        boolean weekString;
        switch (week) {
            case Calendar.SUNDAY:
                return  true;
            case Calendar.MONDAY:
                return  false;
            case Calendar.TUESDAY:
                return  false;
            case Calendar.WEDNESDAY:
                return  false;
            case Calendar.THURSDAY:
                return  false;
            case Calendar.FRIDAY:
                return  false;
            default:
                return  true;

        }
    }
    public String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }


}
